package com.tansun.easycare.rule.excel.handle.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.tansun.easycare.rule.excel.handle.BaseHandle;
import com.tansun.easycare.rule.excel.utils.HtmlRegexpUtil;
import com.tansun.easycare.rule.rulegroup.entity.RuleGroup;

/**
 * 规则导出导出的处理类 20191229 ljt
 */
public class GeneralRuleHandle extends BaseHandle<RuleGroup> {
    @Override
    public void initHandleTemplate() {
        super.initHandleTemplate();
    }

    @Override
    public void createMergedCell(Sheet sheet, int firstRow, int firstCol, int lastRow, int lastCol, String val) {
        super.createMergedCell(sheet, firstRow, firstCol, lastRow, lastCol, val);
    }

    @Override
    protected Row getRow(Sheet sheet, int num) {
        return super.getRow(sheet, num);
    }

    @Override
    protected Cell getCell(Row row, int num) {
        return super.getCell(row, num);
    }

    @Override
    protected Cell getCell(Sheet sheet, int rowNum, int colNum) {
        return super.getCell(sheet, rowNum, colNum);
    }

    @Override
    public List<RuleGroup> excelToEntity(Workbook wb, String projectCode,String userId) {
        return null;
    }

    @Override
    public Workbook entityToExcel(List<RuleGroup> list) {
        //在内存中创建一个execl文件
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook();
        //创建工作蒲
        XSSFSheet sheet = xssfWorkbook.createSheet("规则");
        sheet.setColumnWidth(0,20*256);
        sheet.setColumnWidth(1,20*256);
        sheet.setColumnWidth(2,20*256);
        sheet.setColumnWidth(3,70*256);
        sheet.setColumnWidth(4,70*256);
        sheet.setColumnWidth(5,70*256);
        //创建标题行
        XSSFRow titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("RULE_NAME");
        titleRow.createCell(1).setCellValue("PROJECT_CODE");
        titleRow.createCell(2).setCellValue("VERSION");
        titleRow.createCell(3).setCellValue("RULESCORE");
        titleRow.createCell(4).setCellValue("REASONCODE");
        titleRow.createCell(5).setCellValue("FACTS");
        titleRow.createCell(6).setCellValue("EVALS");
        titleRow.createCell(7).setCellValue("ACTIONS");
        
        for(RuleGroup ruleGroup:list) {
        	int lastRowNum = sheet.getLastRowNum();
            XSSFRow contentRow = sheet.createRow(lastRowNum+1);
            contentRow.createCell(0).setCellValue(ruleGroup.getRuleName());
            contentRow.createCell(1).setCellValue(ruleGroup.getProjectCode());
            contentRow.createCell(2).setCellValue(ruleGroup.getVersion());
            contentRow.createCell(3).setCellValue(ruleGroup.getRuleScore());
            contentRow.createCell(4).setCellValue(ruleGroup.getReasonCode());
            String facts = ruleGroup.getFacts();
            if(StringUtils.isNotEmpty(facts)) {
            	contentRow.createCell(5).setCellValue(HtmlRegexpUtil.filterHtml(facts));
            }
            String evals = ruleGroup.getEvals();
            if(StringUtils.isNotEmpty(evals)) {
            	contentRow.createCell(6).setCellValue(HtmlRegexpUtil.filterHtml(evals));
            }
            String actions = ruleGroup.getActions();
            if(StringUtils.isNotEmpty(actions)) {
            	contentRow.createCell(7).setCellValue(HtmlRegexpUtil.filterHtml(actions));
            }
        }
        return xssfWorkbook;
    }
}
